<?php
/***************************************************************************
 *   Copyright (C) 2007 by Andrea Blankenstijn, David Dieulivol            *
 *   darkan9el@gmail.com, daviddieulivol5@hotmail.com                      *
 *                                                                         *
 *   All rights reserved.                                                  *
 *   This work is released under the terms of the New BSD License.         *
 *   See LICENSE.BSD file for licensing details                            *
 ***************************************************************************/
/* * *
 * $Id: addPhotos.php 49 2007-08-27 18:23:57Z DarkAn9el $
 * * */

@$action = $_GET['action'];

if($action == 'send')
{
	if(@$_FILES['imgfile']['error'] !== UPLOAD_ERR_OK)
		die('Erreur durant l\'upload du fichier.');

	@$author = (int) $_POST['author'];
	@$copy   = (int) $_POST['copyright'];
	@$desc   = htmlspecialchars(trim($_POST['desc']));
	@$file   = $_FILES['imgfile']['tmp_name'];
	@$online = (int) $_POST['isOnline'];
	@$name   = htmlspecialchars(trim($_POST['name']));

	if(!is_int($author) OR !is_int($copy) OR empty($desc) OR !is_file($file) OR !is_int($online) OR empty($name))
		die('Champ(s) invalide(s)');

	try
	{
		$admin = new AdminPhoto($odb);
		$imgId = $admin->addPhoto($name, $desc, $author, $copy, $file, $online);
		$admin = new AdminGallery($odb);
		$admin->updateGallery(1, null, null, null, array($imgId));
	}
	catch(Exception $e)
	{
		die('Exception de AdminPhoto : '. $e->getMessage());
	}

	echo '<p>Photo enregistrée, tentative d\'affichage des données enregistrées</p>';

	try
	{
		$photo = new Photo($odb, $imgId);

		$desc  = $photo->getDescription();
		$file  = $photo->getThumbnailWebPath();
		$name  = $photo->getName();
	
		echo '<h1>', $name,'</h1>';
		echo '<img src="', $file,'" alt="', $name,'"/>';
		echo '<p><em>', $desc,'</em></p>';
	}
	catch(Exception $e)
	{
		die('Erreur de Photo : '. $e->getMessage());
	}
	
}
else
{
?>

<form action="?addPhoto&amp;action=send" method="post" enctype="multipart/form-data">
	<p>Ajouter une image</p>

	<label>Fichier&nbsp;: </label>
	<input type="file" name="imgfile" size="30" maxlength="1000" accept="image/*" /><br />

	<label>Nom&nbsp;:</label>
	<input type="text" name="name" size="30" maxlength="40" /><br />

	<label>Description&nbsp;:</label><br/>
	<textarea name="desc" rows="10" cols="50" wrap="on">
	</textarea><br/>

	<p>
		<label>Auteur&nbsp;:</label>
		<select name="author" size="1">
			<?php
			// pas de classe actuellement pour faire ce travail
			if($query = $odb->query('SELECT * FROM `ug_authors`'))
			{
				while($author = $query->fetch())
				{
					echo '<option value="', $author['idAuthors'],'">';
					echo $author['firstname'], ' ', $author['lastname'];
					echo '</option>';
				}
			}
			?>
		</select>

		<label>Copyright&nbsp;:</label>
		<select name="copyright" size="1">
			<?php
			// pas de classe actuellement pour faire ce travail
			if($query = $odb->query('SELECT `idCopyrights`,`name` FROM `ug_copyrights`'))
			{
				while($copy = $query->fetch())
				{
					echo '<option value="', $copy['idCopyrights'],'">';
					echo $copy['name'];
					echo '</option>';
				}
			}
			?>
		</select>

		<label>En ligne&nbsp;:&nbsp;</label>
		<input type="checkbox" name="isOnline" value="1" />
	</p>

	<input type="submit" />  
</form>

<p><em>$Id: addPhotos.php 49 2007-08-27 18:23:57Z DarkAn9el $</em></p>

<?php
}
?>